package kt.com.bilin.coin.common

import com.bilin.coin.BuildConfig
import kt.com.bilin.coin.utils.SPUtils
import java.util.*

object ServiceConfig {
    var API: String = ""
    var H5: String? = null
    var TOTAL_SERVICE_TAG: String? = null
    var API_SERVICE_TAG: String? = null
    var H5_SERVICE_TAG: String? = null
    private val SERVICE_CONFIGS: MutableList<ServiceConfigBean> = mutableListOf()
    private var CURRENT_SERVICE_CONFIG: CurrentServiceConfig? = null
    val RELEASE = ServiceConfigBean(
        1,
        "正式",
        "release",
        "pro",
        "https://h5new.naboyi.com/?test=false",  //h5.naboyi.com
        "https://blbk.cc"
    )
    private val UAT = ServiceConfigBean(
        2,
        "外网测试",
        "uat",
        "test",
        "http://h5.szgaodashang.com:8080/?test=true",  //http://h5.szgaodashang.com:8080/?test=true   https://h5new.naboyi.com/?test=false
        "http://180782y7x0.51mypc.cn"
    )
    private val TEST = ServiceConfigBean(
        3,
        "本地测试",
        "debug",
        "test",
        "http://11.241.12.137:8081/?test=true",
        "http://180782y7x0.51mypc.cn"
    )

    init {
        val declaredFields = ServiceConfig::class.java.declaredFields
        for (declaredField in declaredFields) {
            if (declaredField.type == ServiceConfigBean::class.java) {
                try {
                    SERVICE_CONFIGS.add(declaredField[this] as ServiceConfigBean)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        SERVICE_CONFIGS.sortWith(Comparator { o1: ServiceConfigBean, o2: ServiceConfigBean -> o1.order - o2.order })
    }

    fun reset() {
        CURRENT_SERVICE_CONFIG = null
    }

    fun getCurrentServiceConfig(): CurrentServiceConfig {
        if (CURRENT_SERVICE_CONFIG == null) {
            val saveLoca: Boolean = SPUtils.getBoolean(SP_KEY_SAVE_LOCA, false)
            val defaultServiceConfig: ServiceConfigBean = getDefaultServiceConfig()
            val currentServiceConfig: CurrentServiceConfig
            if (saveLoca) {
                currentServiceConfig = CurrentServiceConfig()
                currentServiceConfig.pushTag = defaultServiceConfig.pushTag
                currentServiceConfig.apiUrl = SPUtils.getString(SP_KEY_API)
                currentServiceConfig.h5Url = SPUtils.getString(SP_KEY_H5)
                currentServiceConfig.totalServiceTag = SPUtils.getString(SP_KEY_TOTAL_SERVICE_TAG)
                currentServiceConfig.apiServiceTag = SPUtils.getString(SP_KEY_API_SERVICE_TAG)
                currentServiceConfig.h5ServiceTag = SPUtils.getString(SP_KEY_h5_SERVICE_TAG)
                return currentServiceConfig
            } else {
                currentServiceConfig = CurrentServiceConfig()
                currentServiceConfig.pushTag = defaultServiceConfig.pushTag
                currentServiceConfig.apiUrl = if (API.isEmpty()) defaultServiceConfig.apiUrl else API
                currentServiceConfig.h5Url = if (H5 == null) defaultServiceConfig.h5Url else H5
                currentServiceConfig.totalServiceTag = if (TOTAL_SERVICE_TAG == null) defaultServiceConfig.serviceTag else TOTAL_SERVICE_TAG
                currentServiceConfig.apiServiceTag = if (API_SERVICE_TAG == null) defaultServiceConfig.serviceTag else API_SERVICE_TAG
                currentServiceConfig.h5ServiceTag = if (H5_SERVICE_TAG == null) defaultServiceConfig.serviceTag else H5_SERVICE_TAG
            }
            CURRENT_SERVICE_CONFIG = currentServiceConfig
        }
        return CURRENT_SERVICE_CONFIG!!
    }

    private fun getDefaultServiceConfig(): ServiceConfigBean {
        var defaultServiceConfig: ServiceConfigBean? = null
        for (serviceConfig in SERVICE_CONFIGS) {
            if (serviceConfig.serviceTag == BuildConfig.BUILD_TYPE) defaultServiceConfig = serviceConfig
        }
        if (defaultServiceConfig == null) defaultServiceConfig = UAT
        return defaultServiceConfig
    }

    fun getServiceConfigs(): MutableList<ServiceConfigBean> {
        val serviceConfigs: MutableList<ServiceConfigBean> = mutableListOf()
        for (serviceConfig in SERVICE_CONFIGS) {
            serviceConfigs.add(serviceConfig.copy())
        }
        return serviceConfigs
    }
}

open class ServiceConfigBean(
    var order: Int = 0,
    var serviceName: String? = null,
    var serviceTag: String? = null,
    var pushTag: String? = null,
    var h5Url: String? = null,
    var apiUrl: String = "",
    var check: Boolean = false
) {
    open fun copy(): ServiceConfigBean = ServiceConfigBean(order, serviceName, serviceTag, pushTag, h5Url, apiUrl)
}

class CurrentServiceConfig(
    var totalServiceTag: String? = null,
    var apiServiceTag: String? = null,
    var h5ServiceTag: String? = null
) : ServiceConfigBean()